<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 17:51
 */

namespace app\admin\model;


class ChatGroup extends DataBase
{
    protected  $name = 'chat_group';
    protected $autoWriteTimestamp = true;


    public function getExpireTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getDataList()
    {
        $page_config = [
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        ];

        $where = '';
        if(!empty(input('get.key')) && !empty(input('get.value'))){
            $key = input('get.key'); $value = input('get.value');
            $owner_id = User::where($key,'=',$value)->value('id');
            $where[] = ['owner_id','=',$owner_id];
        }

        $data = $this->where($where)->paginate($page_config);

        foreach($data as $key=>$value){

            $data[$key]['u_id'] = User::where('id','=',$value['owner_id'])->value('u_id');
            if($value['announcement'])
                // halt(json_decode($value['announcement'],true));
                $data[$key]['announcement'] =implode('%', json_decode($value['announcement'],true));
        }
        return $data;
    }

    public function admin()
    {
        $id = input('get.id');
        $admin_users = $this->where('id','=',$id)->value('admins');

        $admin_users = json_decode($admin_users,true);

        if($admin_users){

            foreach($admin_users as $key=>$value){
                //   halt($value);
                $userInfo = User::where('id','=',$value)->find();
                // halt($userInfo);
                $arr['u_id'] = $userInfo['u_id'];
                $arr['nickname'] = $userInfo['nickname'];
                $admin_users[$key] = $arr;
            }

        }
        return [$admin_users,$id];
    }

    public function adminAdd()
    {
        $id = input('get.id');
        $u_id = input('get.u_id');
        //halt($u_id);
        $admin_users = $this->where('id','=',$id)->value('admins');
        $admin_users = json_decode($admin_users,true);

        $user_id = User::where('u_id','=',$u_id)->value('id');

        if(is_array($admin_users)) array_push($admin_users,$user_id);
        else $admin_users = [$user_id];

        $admins = json_encode($admin_users);

        $res = $this->isUpdate(true)->save(['id' => $id, 'admins' => $admins]);

        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100, 'msg'=>'添加管理员失败'];

    }

    public function edit()
    {
        $id = input('get.id');
        $announcement = input('get.announcement');
        if(!$announcement)  return ['code'=>-100,'msg'=>'公告不能为空'];
        $announcement = explode('%',$announcement);
        $announcement = json_encode($announcement);

        $res = $this->isUpdate(true)->save(['id' => $id, 'announcement' => $announcement]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100, 'msg'=>'修改失败'];
    }
}